package at.jku.se.fastsell.gui;

import at.jku.se.fastsell.FastsellUI;

import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.server.VaadinSession;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.themes.Reindeer;

/**
 * Master view for logged in FastSell users
 * Extend this class for all logged-in user views
 */
public class MainView extends VerticalLayout implements View {

	public static final String NAME = "Main";

	private static final long serialVersionUID = 1L;

	private HorizontalLayout topLayout, logoutLayout;
	private VerticalLayout headingLayout;
	private Panel contentPanel;
	private Label lblHead, lblInfo, lblUser;
	private Button btnLogout;
	
	private String user;
	
	private IRefreshable refreshCallback;

	// ----------------------------------------------------------------------------------

	public MainView() {
		this.setSizeFull();
		this.setSpacing(true);
		this.setMargin(true);
		this.buildMainLayout();
	}

	// ----------------------------------------------------------------------------------

	private void buildMainLayout() {
		topLayout = new HorizontalLayout();
		topLayout.setWidth("100%");
		this.addComponent(topLayout);
		
		headingLayout = new VerticalLayout();
		topLayout.addComponent(headingLayout);
		
		lblHead = new Label("Fast$ell: The Auction Platform");
		lblHead.setStyleName(Reindeer.LABEL_H1);
		headingLayout.addComponent(lblHead);

		lblInfo = new Label("Service Engineering (259028/2014S) - Group 6");
		lblInfo.setStyleName(Reindeer.LABEL_SMALL);
		headingLayout.addComponent(lblInfo);
	
		logoutLayout = new HorizontalLayout();
		logoutLayout.setSpacing(true);
		logoutLayout.setMargin(true);
		topLayout.addComponent(logoutLayout);
		topLayout.setComponentAlignment(logoutLayout, Alignment.TOP_RIGHT);
		
		lblUser = new Label();
		logoutLayout.addComponent(lblUser);
		logoutLayout.setComponentAlignment(lblUser, Alignment.MIDDLE_CENTER);

		btnLogout = new Button("Logout");
		btnLogout.addClickListener(new ClickListener() {

			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				VaadinSession.getCurrent().getSession().removeAttribute("user");
				FastsellUI.navigateTo(EntryView.NAME);
			}
		});
		logoutLayout.addComponent(btnLogout);
		
		contentPanel = new Panel();
		contentPanel.setStyleName(Reindeer.PANEL_LIGHT);
		this.addComponent(contentPanel);
		this.setExpandRatio(contentPanel, 1);
	}

	// ----------------------------------------------------------------------------------

	/**
	 * Sets component for content area
	 * @param content - Component for content area
	 */
	public void setContent(Component content) {
		contentPanel.setContent(content);
	}

	/**
	 * Sets component for content area with a callback for refresh
	 * @param content - Component for content area
	 * @param refreshCallback - Callback to refresh content
	 */
	public void setContent(Component content, IRefreshable refreshCallback) {
		this.setContent(content);
		this.refreshCallback = refreshCallback;
	}

	// ----------------------------------------------------------------------------------

	public String getUser() {
		return user;
	}
	
	protected void setUser(String user) {
		this.user = user;
	}
	
	// ----------------------------------------------------------------------------------
	
	@Override
	public void enter(ViewChangeEvent event) {
		Object user = VaadinSession.getCurrent().getSession().getAttribute("user");
		
		if (user == null || user.toString().equals("")) {
			FastsellUI.navigateTo(EntryView.NAME);
		} else {
			lblUser.setValue("Hello " + user.toString() + "!");
			
			this.setUser(user.toString());
			
			if (refreshCallback != null) {
				refreshCallback.refreshData();
			}
		}
	}

}
